import 'package:flutter/material.dart';

import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class RatingChartWidget extends StatelessWidget {
  static const Color kLightStar = Color(0xffffc900);
  static const Color kGreyStar = Color(0xffa3a5a7);
  static const double kStarSize = 8;

  final List<double> chartDatas;

  const RatingChartWidget({Key? key, required this.chartDatas})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(chartDatas.length, _buildRow),
      ),
    );
  }

  Widget _buildRow(int index) {
    int length = chartDatas.length;
    return Container(
      padding: EdgeInsets.symmetric(vertical: 2),
      child: Row(
        children: <Widget>[
          _buildStar(length - index),
          _buildBar(index),
        ],
      ),
    );
  }

  Widget _buildStar(int count) {
    return Container(
      width: 50,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: List.generate(
          count,
          (index) {
            return Icon(
              FontAwesomeIcons.solidStar,
              color: kGreyStar,
              size: kStarSize,
            );
          },
        ),
      ),
    );
  }

  Widget _buildBar(int index) {
    return Expanded(
      child: Container(
        padding: EdgeInsets.only(left: 5),
        constraints: BoxConstraints.tightFor(height: 6.0),
        child: Stack(
          children: <Widget>[
            Positioned(
              child: Container(
                decoration: BoxDecoration(
                  color: kGreyStar,
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
            ),
            Positioned(
              child: Container(
                constraints: BoxConstraints.tightFor(width: chartDatas[index]),
                decoration: BoxDecoration(
                  color: kLightStar,
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
